﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class DlgUsedSupplyWater : System.Windows.Forms.Form
	{
		public DlgUsedSupplyWater()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.InitCalcuList();
			this.calculateOjects = new List<UsedWaterCalculate>();
			UsedWaterCalculate usedWaterCalculate = new AirConditionUsedCalculate("空调补水");
			usedWaterCalculate.usedTimeProposal = 24.0;
			usedWaterCalculate.coefficientProposal = 1.2;
			this.calculateOjects.Add(usedWaterCalculate);
			this.UnforeseenValue.Text = "10";
			this.IsCalculated = false;
			base.CancelButton = this.buttonCancel;
		}

		private void InitCalcuList()
		{
			string[] array = new string[]
			{
				"",
				"用水部位名称",
				"用水标准",
				"单 位",
				"数 量",
				"用水时间(h)",
				"变化系数",
				"最大日(m3/d)",
				"最大时(m3/h)",
				"平均时(m3/h)"
			};
			for (int i = 0; i < array.Length; i++)
			{
				int num = array[i].Length;
				if (array[i].IndexOf('(') > 0)
				{
					num = array[i].IndexOf("(") + 2;
				}
				this.CalcuResultList.Columns.Add(array[i], num * 19, HorizontalAlignment.Center);
			}
			this.CalcuResultList.notification = new EidtListView.DataChangedNotification(this.changeDataManually);
		}

		private void AddType_Click(object sender, EventArgs e)
		{
			DlgUsedWaterType dlgUsedWaterType = new DlgUsedWaterType();
			dlgUsedWaterType.ShowDialog(this);
			UsedWaterCalculate calculate = dlgUsedWaterType.calculate;
			if (calculate != null)
			{
				double usedCount = calculate.usedCount;
				if (this.calculateOjects.Count > 0)
				{
					this.calculateOjects.Insert(this.calculateOjects.Count - 1, calculate);
				}
				else
				{
					this.calculateOjects.Add(calculate);
				}
				if (!this.Calculate())
				{
					this.calculateOjects.Remove(calculate);
				}
			}
		}

		private void UpdateResultList()
		{
			this.CalcuResultList.Items.Clear();
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			for (int i = 0; i < this.calculateOjects.Count - 1; i++)
			{
				UsedWaterCalculate usedWaterCalculate = this.calculateOjects[i];
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.Tag = i;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.typeName;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.maxUsedProposal.ToString();
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.unit;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.usedCount.ToString();
				listViewSubItem.Tag = 3;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.usedTimeProposal.ToString();
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.coefficientProposal.ToString();
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.maxUsedPerDay.ToString("F2");
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.maxUsedPerHour.ToString("F2");
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate.aveUserPerHour.ToString("F2");
				listViewItem.SubItems.Add(listViewSubItem);
				this.CalcuResultList.Items.Add(listViewItem);
				num += usedWaterCalculate.maxUsedPerDay;
				num2 += usedWaterCalculate.maxUsedPerHour;
				num3 += usedWaterCalculate.aveUserPerHour;
			}
			if (this.IsCalculated && this.calculateOjects.Count > 1)
			{
				UsedWaterCalculate usedWaterCalculate2 = this.calculateOjects[this.calculateOjects.Count - 1];
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.Tag = this.calculateOjects.Count - 1;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.typeName;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = "";
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.unit;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = "";
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.usedTimeProposal.ToString();
				listViewSubItem.Tag = 4;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.coefficientProposal.ToString();
				listViewSubItem.Tag = 5;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.maxUsedPerDay.ToString("F2");
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.maxUsedPerHour.ToString("F2");
				listViewSubItem.Tag = 7;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = usedWaterCalculate2.aveUserPerHour.ToString("F2");
				listViewItem.SubItems.Add(listViewSubItem);
				num += usedWaterCalculate2.maxUsedPerDay;
				num2 += usedWaterCalculate2.maxUsedPerHour;
				num3 += usedWaterCalculate2.aveUserPerHour;
				this.CalcuResultList.Items.Add(listViewItem);
				double num4 = num * Convert.ToDouble(this.UnforeseenValue.Text) / 100.0;
				double num5 = num2 * Convert.ToDouble(this.UnforeseenValue.Text) / 100.0;
				double num6 = num3 * Convert.ToDouble(this.UnforeseenValue.Text) / 100.0;
				string[] items = new string[]
				{
					"",
					"未预见水量",
					this.UnforeseenValue.Text,
					"%",
					"",
					"",
					"",
					num4.ToString("F2"),
					num5.ToString("F2"),
					num6.ToString("F2")
				};
				this.CalcuResultList.Items.Add(new ListViewItem(items));
				num += num4;
				num2 += num5;
				num3 += num6;
				string[] items2 = new string[]
				{
					"",
					"总计",
					"",
					"",
					"",
					"",
					"",
					num.ToString("F2"),
					num2.ToString("F2"),
					num3.ToString("F2")
				};
				this.CalcuResultList.Items.Add(new ListViewItem(items2));
			}
		}

		private void WriteResultToFile_Click(object sender, EventArgs e)
		{
			try
			{
				HYSystemCal hysystemCal = new HYSystemCal();
				hysystemCal.systemType = HYSystemCal.SystemType.UnDefine;
				ExcelData excelData = this.GetExcelData();
				new CalExportExcelProgressBar(hysystemCal, excelData).ShowDialog();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.ToString());
			}
		}

		private ExcelData GetExcelData()
		{
			List<ExcelData.Cell> list = new List<ExcelData.Cell>();
			List<List<ExcelData.Cell>> list2 = new List<List<ExcelData.Cell>>();
			for (int i = 0; i < this.CalcuResultList.Columns.Count; i++)
			{
				if (i != 0)
				{
					list.Add(new ExcelData.Cell
					{
						TypeOfCell = ExcelData.ExcelCellType.StringCell,
						Data = this.CalcuResultList.Columns[i].Text
					});
				}
			}
			for (int j = 0; j < this.CalcuResultList.Items.Count; j++)
			{
				List<ExcelData.Cell> list3 = new List<ExcelData.Cell>();
				list3.Add(new ExcelData.Cell
				{
					TypeOfCell = ExcelData.ExcelCellType.StringCell,
					Data = this.CalcuResultList.Items[j].SubItems[1].Text
				});
				string text = this.CalcuResultList.Items[j].SubItems[2].Text;
				ExcelData.Cell cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				list3.Add(new ExcelData.Cell
				{
					TypeOfCell = ExcelData.ExcelCellType.StringCell,
					Data = this.CalcuResultList.Items[j].SubItems[3].Text
				});
				text = this.CalcuResultList.Items[j].SubItems[4].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[5].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[6].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 1;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[7].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[8].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[9].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				list2.Add(list3);
			}
			return new ExcelData
			{
				SheetName = "最高日最高时用水量计算",
				FileName = "最高日最高时用水量计算",
				Title = "最高日最高时用水量",
				ColumnsData = list,
				RowsData = list2
			};
		}

		private void CalcuResultList_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			this.IsCalculated = true;
		}

		private void buttonDelete_Click(object sender, EventArgs e)
		{
			for (int i = this.CalcuResultList.SelectedItems.Count - 1; i >= 0; i--)
			{
				ListViewItem listViewItem = this.CalcuResultList.SelectedItems[i];
				if (listViewItem.Tag != null && (int)listViewItem.Tag != this.calculateOjects.Count - 1)
				{
					this.calculateOjects.RemoveAt((int)listViewItem.Tag);
					listViewItem.Remove();
				}
			}
			if (this.calculateOjects.Count == 1)
			{
				this.CalcuResultList.Items.Clear();
				this.IsCalculated = false;
				AirConditionUsedCalculate airConditionUsedCalculate = (AirConditionUsedCalculate)this.calculateOjects[0];
				if (airConditionUsedCalculate != null)
				{
					airConditionUsedCalculate.usedTimeProposal = 24.0;
					airConditionUsedCalculate.coefficientProposal = 1.2;
					airConditionUsedCalculate.coefficientProposal = 0.0;
					return;
				}
			}
			else if (this.calculateOjects.Count > 1)
			{
				this.Calculate();
			}
		}

		private void changeDataManually(string text, int itemTag, int subItemTag)
		{
			bool flag = false;
			double num = 0.0;
			try
			{
				num = Convert.ToDouble(text);
				flag = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			if (flag && itemTag > -1 && itemTag < this.calculateOjects.Count)
			{
				UsedWaterCalculate usedWaterCalculate = this.calculateOjects[itemTag];
				switch (subItemTag)
				{
				case 3:
					usedWaterCalculate.usedCount = num;
					break;
				case 4:
					usedWaterCalculate.usedTimeProposal = num;
					break;
				case 5:
					usedWaterCalculate.coefficientProposal = num;
					break;
				case 7:
					usedWaterCalculate.maxUsedPerHour = num;
					break;
				}
				this.Calculate();
			}
		}

		private void buttonClear_Click(object sender, EventArgs e)
		{
			this.CalcuResultList.Items.Clear();
			for (int i = this.calculateOjects.Count - 2; i > -1; i--)
			{
				this.calculateOjects.RemoveAt(i);
			}
			this.IsCalculated = false;
			AirConditionUsedCalculate airConditionUsedCalculate = (AirConditionUsedCalculate)this.calculateOjects[this.calculateOjects.Count - 1];
			if (airConditionUsedCalculate != null)
			{
				airConditionUsedCalculate.usedTimeProposal = 24.0;
				airConditionUsedCalculate.coefficientProposal = 1.2;
				airConditionUsedCalculate.coefficientProposal = 0.0;
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonCalculate_Click(object sender, EventArgs e)
		{
			this.Calculate();
		}

		private bool Calculate()
		{
			bool result = false;
			bool flag = false;
			try
			{
				Convert.ToDouble(this.UnforeseenValue.Text);
				flag = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
			if (flag)
			{
				for (int i = 0; i < this.calculateOjects.Count; i++)
				{
					this.calculateOjects[i].Calculate();
				}
				this.IsCalculated = true;
				this.UpdateResultList();
				result = true;
			}
			return result;
		}

		private List<UsedWaterCalculate> calculateOjects;

		private bool IsCalculated;
	}
}
